﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;
using Skyworth.CRM.Service.Entity;

namespace Skyworth.CRM.Web.CustomerManagement
{
    public partial class EditSalesActive : BasePage
    {
        private ICustActivitiesBLL activeBLL = IocContainer.Instance.Resolve<ICustActivitiesBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            SystemOverTime();
            if (!IsPostBack)
            {               
                this.txtDate.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                txtActiveDate.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int activeId = 0;
                    int.TryParse(Request.QueryString["ActiveId"], out activeId);
                    if (activeId > 0)
                    {
                        DataTable dt = activeBLL.Query(string.Format(" and tca.FItemId={0}", activeId));//.GetModel(activeId);
                        if (dt.Rows.Count > 0)
                        {
                            DataRow dr = dt.Rows[0];
                            this.txtActiveCode.Text = dr["FNumber"].ToString();// model.FNumber;
                            this.hidBussId.Value = dr["FBusinessoppId"].ToString();
                            this.txtBussCode.Text = dr["BssCode"].ToString(); //model.FBusinessoppId.ToString();
                            base.BinDropDownList<DropDownList>(this.ddlstActiveType, dr["FAddress"].ToString(), true);
                            this.txtActiveDate.Text = dr["FOppDate"].ToString(); //model.FOppDate.ToString("yyyy-MM-dd");
                            this.txtDate.Text = dr["FDate"].ToString(); //model.FDate.ToString("yyyy-MM-dd");
                            this.txtNote.Text = dr["FNote"].ToString(); //model.FNote;
                            this.txtPrototypeProgress.Text = dr["PrototypeProgress"].ToString();
                            this.txtOrdereProgress.Text = dr["OrdereProgress"].ToString();
                            this.txtFeedback.Text = dr["Feedback"].ToString();
                            this.txtNormalMaintenance.Text = dr["NormalMaintenance"].ToString();
                        }
                    }
                }
                else
                {
                    // 销售线索过来的新增
                    if (Request.QueryString["LeadId"] != null && Request.QueryString["LeadCode"] != null)
                    {
                        this.hidBussId.Value = Request.QueryString["LeadId"];
                        this.txtBussCode.Text = Request.QueryString["LeadCode"];
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Boolean isOK = false;
                CustActivities model = new CustActivities();
                model.FBusinessoppId = Convert.ToInt32(this.hidBussId.Value.Trim());
                model.FAddress = Convert.ToInt32(this.ddlstActiveType.SelectedValue.Trim());
                model.FOppDate = Convert.ToDateTime(this.txtActiveDate.Text.Trim());
                model.FDate = Convert.ToDateTime(this.txtDate.Text.Trim());
                model.FMaker = Int32.Parse(base.UserID);
                model.FNote = this.txtNote.Text.Trim();
                model.FSubmitDate = System.DateTime.Now;
                model.PrototypeProgress = this.txtPrototypeProgress.Text.Trim();
                model.OrdereProgress = this.txtOrdereProgress.Text.Trim();
                model.Feedback = this.txtFeedback.Text.Trim();
                model.NormalMaintenance = this.txtNormalMaintenance.Text.Trim();
                if ("add" == Request.QueryString["Type"].ToString().ToLower())
                {
                    this.txtActiveCode.Text = activeBLL.Add("ACT", model);
                    isOK = true;
                }
                else if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int activeId = 0;
                    int.TryParse(Request.QueryString["ActiveId"], out activeId);
                    model.FItemId = activeId;
                    activeBLL.Update(model);
                    isOK = true;
                }
                if (isOK)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "closeThisPage", "<script language=\"JavaScript\">parent.addWindow.close();parent.location.href = parent.location.href;</script>");
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
        }
    }
}